package com.mcjtf.prefix.commands;

import com.mcjtf.prefix.PrefixManager;
import com.mcjtf.prefix.api.event.PrefixChangeEvent;
import com.mcjtf.prefix.model.User;
import com.mcjtf.prefix.service.ConfigService;
import com.mcjtf.prefix.service.PrefixService;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class PlayerCommand {
    private PrefixManager main;
    private PrefixService prefixService;

    public PlayerCommand() {
        main = PrefixManager.getInstance();
        prefixService = main.getPrefixService();
    }

    @SubCommand(cmd = "set", usage = "[prefix/suffix] [称号序号]", desc = "设置使用的前缀/后缀", permission = "prefixmanager.set", min = 3, max = 3, sender = SubCommand.SenderType.PLAYER)
    public void set(Player sender, String[] args) {
        if (!main.isInt(args[2]) || !argsMatch(args[1])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "参数错误.");
            return;
        }
        User user = prefixService.getUser(sender.getName());
        int id = Integer.parseInt(args[2]);

        PrefixChangeEvent call = null;

        if (args[1].equalsIgnoreCase("prefix") || args[1].equalsIgnoreCase("p")) {
            if (user.getPrefixes().size() <= id) {
                sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "无效的称号序号");
                sender.sendMessage(main.msg("&b输入&6/prm list p &b查看当前可用前缀"));
                return;
            }
            user.setUsePrefix(id);
            sender.sendMessage(ConfigService.PREFIX + main.msg("&a当前前缀已经被设置为:" + user.getPrefixes().get(id)));
            call = new PrefixChangeEvent(sender, "prefix", user.getPrefixes().get(id));
        } else {
            if (user.getSuffixes().size() <= id) {
                sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "无效的称号序号");
                sender.sendMessage(main.msg("&b输入&6/prm list s &b查看当前可用后缀"));
                return;
            }
            user.setUseSuffix(id);
            sender.sendMessage(ConfigService.PREFIX + main.msg("当前后缀已经被设置为:" + user.getSuffixes().get(id)));
            call = new PrefixChangeEvent(sender, "suffix", user.getSuffixes().get(id));
        }
        main.getServer().getPluginManager().callEvent(call);
    }

    @SubCommand(cmd = "list", usage = "[prefix/suffix]", desc = "显示可用的前缀/后缀", permission = "prefixmanager.list", min = 2, max = 2, sender = SubCommand.SenderType.PLAYER)
    public void list(Player sender, String[] args) {
        if (!argsMatch(args[1])) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "参数错误.");
            return;
        }
        User user = main.getPrefixService().getUser(sender.getName());
        if (user == null) {
            sender.sendMessage(ConfigService.PREFIX + ChatColor.RED + "无法载入称号数据,请稍后再试.");
            return;
        }
        prefixService.list(sender, user, args[1]);
    }

    private boolean argsMatch(String arg) {
        if (arg.equalsIgnoreCase("prefix") || arg.equalsIgnoreCase("p")) {
            return true;
        }
        if (arg.equalsIgnoreCase("suffix") || arg.equalsIgnoreCase("s")) {
            return true;
        }
        return false;
    }
}
